package com.hongyuan.sellfitness.ui.home.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.RxBus;
import com.hongyuan.mvvmhabitx.bus.RxSubscriptions;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.data.MyRepository;
import com.hongyuan.sellfitness.ui.home.beans.StoreGroupDetailBean;
import com.hongyuan.sellfitness.ui.main.activity.WebActivity;
import com.hongyuan.sellfitness.ui.main.beans.ScanBeans;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class StoreGroupDetailViewModel extends CustomViewModel<MyRepository> {

    private Disposable mSubscription;

    public ObservableField<StoreGroupDetailBean> entity = new ObservableField<>();

    public StoreGroupDetailViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //给RecyclerView添加ObservableList
    public ObservableList<GroupDetailsItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<GroupDetailsItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_group_details_images);

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //底部按钮状态UI处理
        public SingleLiveEvent<StoreGroupDetailBean> changeShow = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> qdSuccess = new SingleLiveEvent<>();
        public SingleLiveEvent<Void> changeRemind = new SingleLiveEvent<>();
    }

    //团课规则
    public BindingCommand goH5 = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString("url","http://www.hongyuangood.com/signUpRule/index.html");
        bundle.putString("title","团课报名规则");
        startActivity(WebActivity.class,bundle);
    });

    @Override
    public void onCreate() {
        super.onCreate();

        mSubscription = RxBus.getDefault().toObservable(ScanBeans.class)
                .observeOn(AndroidSchedulers.mainThread()) //回调到主线程更新UI
                .subscribe(new Consumer<ScanBeans>() {
                    @Override
                    public void accept(ScanBeans scanBeans) throws Exception {
                        //扫码签到
                        setParams("ocs_id",String.valueOf(entity.get().getOcs_id()))
                                .setParams("type","qd");
                        requestData(Constants.SUPER_COURSE_XY_QD);
                    }
                });
        //将订阅者加入管理站
        RxSubscriptions.add(mSubscription);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

        //取消订阅，防止内存泄漏
        RxSubscriptions.remove(mSubscription);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.GET_SOURSE_SUPER_INFO){
            StoreGroupDetailBean bean = (StoreGroupDetailBean)dataBean;
            entity.set(bean);
            uc.changeShow.setValue(bean);

            for(StoreGroupDetailBean.MemberOcsBean ocsBean : bean.getMember_ocs()){
                GroupDetailsItemViewModel itemViewModel = new GroupDetailsItemViewModel(this,ocsBean);
                observableList.add(itemViewModel);
            }
        }

        if(code == Constants.SUPER_COURSE_XY_QD){
            uc.qdSuccess.call();
        }

        if(code == Constants.CANCEL_COURSE_SUPER_ORDER){
            //团课以取消预约，再次请求数据并刷新
            requestData(Constants.GET_SOURSE_SUPER_INFO);
        }

        if(code == Constants.DEL_REMIND_CS || code == Constants.ADD_REMIND_CS){
            uc.changeRemind.call();
        }
    }
}
